/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			ETUndoHead.cc
  \date			september 2011
  \author		TNick

  \brief		Contains the implementation for ETUndoHead class


  Copyright (c) 2011, Nicu Tofan
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
 * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<main.h>

#include	<sheet/ETSheetView.h>
#include	<sheet/ETSheet.h>
#include	<sheet/ETUndoElem.h>


#include	"ETUndoHead.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/* ------------------------------------------------------------------------- */
ETUndoHead::ETUndoHead	(ETSheet * sht)
	: CC_HDL2D ()
{
	asc_sheet = sht;
	undo_bf_size = 100; /* a sensible default */
	crt_ptr = NULL;
	redo_count = 0;
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ETUndoHead::~ETUndoHead	( void )
{
	/* the items are iterated and destroyed by the CC_HDL2D::~CC_HDL2D() */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETUndoHead::addUndoItem		(ETUndoElem* new_it)
{

	if (redo_count > 0)
		{
			Q_ASSERT_X((crt_ptr != NULL), "ETUndoHead::performRedo",
					   "To be in a consistent state, since countRedo() > 0, "
					   "the crt_ptr must be non-null.");

			ETUndoElem * itr_el = crt_ptr;
			if (itr_el->next() != NULL)
			{
				itr_el->next()->breakBefore();
			}
			setFirst(itr_el->next());
			setCount(countUndo());
			while (itr_el != NULL)
				{
					ETUndoElem * tmp = itr_el->prev();
					delete itr_el;
					itr_el = tmp;
				}
			redo_count = 0;
		}
	crt_ptr = NULL;
	prepend(new_it);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETUndoHead::performUndo		( int cnt )
{
	if (cnt < 1)
		{
			qDebug() << "Wrong input to ETUndoHead::performUndo: " << cnt;
			return;
		}
	if (countUndo() < 1)
		{
			qDebug() << "ETUndoHead::performUndo has nothing in undo";
			return;
		}

	/* get the item to play with */
	if (crt_ptr == NULL)
		{
			crt_ptr = first();
		}
	else
		{
			crt_ptr = crt_ptr->next();
		}
	Q_ASSERT_X((crt_ptr != NULL), "performUndo",
			   "This should never happen. The crt_ptr is NULL "
			   "when the counters indicate that it should have something.");
	for (;;)
		{
			ETUndoElem * next_to_work = crt_ptr->next();

			/* perform the action */
			crt_ptr->performUndo();

			cnt--;
			redo_count++;
			if (cnt <= 0)
				break;

			if (next_to_work == NULL)
				break;

			crt_ptr = next_to_work;

		}
	/* crt_ptr must point to first item to redo */


}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETUndoHead::performRedo		( int cnt )
{
	if (cnt < 1)
		{
			qDebug() << "Wrong input to ETUndoHead::performRedo: " << cnt;
			return;
		}

	if (countRedo() < 1)
		{
			qDebug() << "ETUndoHead::performRedo has nothing in undo";
			return;
		}

	Q_ASSERT_X(crt_ptr != NULL, "ETUndoHead::performRedo",
			   "To be in a consistent state, since countRedo() > 0, "
			   "the crt_ptr must be non-null.");

	do
		{
			/* perform the action */
			crt_ptr->performRedo();

			crt_ptr = crt_ptr->prev();

			cnt--;
			redo_count--;
			if (cnt <= 0)
				break;

		}
	while (crt_ptr != NULL);

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ETUndoHead::setMaxCount		(int new_max)

{
	undo_bf_size = new_max;

	/* if there are more items than this number then delete the surplus */
	int i_del = countUndo() - undo_bf_size;

	if (i_del > 0)
		{
			ETUndoElem * itr_el = item(undo_bf_size);
			if (itr_el->prev() != NULL)
				{
					itr_el->prev()->breakAfter();
				}
			while (itr_el != NULL)
				{
					ETUndoElem * tmp = itr_el->next();
					delete itr_el;
					itr_el = tmp;
				}

			setCount(undo_bf_size);
		}

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString			ETUndoHead::infoText		( bool in_undo_chain, int index )
{
	if (in_undo_chain)
		{
			return infoText(redo_count + index);
		}
	else
		{
			return infoText(redo_count - index - 1);
		}
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
QString			ETUndoHead::infoText		( int index )
{
	if ((index >= count()) || (index < 0))
		{
			qDebug() << "Wrong input to ETUndoHead::infoText: "
					 << index << " >= " << count();
			return QString();
		}

	return item(index)->description();
}
/* ========================================================================= */



/*  CLASS    =============================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
